package org.example.day04;

/**
 * 第二章节:2.2.7.1:不同线程使用不同锁达不到同步
 */
public class MyThread8 {
    private String anyString = new String();
    public void a(){
        try {
            //线程A用到的锁是anyString对象.
            synchronized (anyString){
                System.out.println("a begin");
                Thread.sleep(3000);
                System.out.println("a end");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    //线程B用到的锁是m8对象
    synchronized public void b(){
        System.out.println("b begin");
        System.out.println("b end");
    }

    public static void main(String[] args) {
        MyThread8 m8 = new MyThread8();
        Thread8A ta = new Thread8A(m8);
        ta.setName("A");
        ta.start();
        Thread8B tb = new Thread8B(m8);
        tb.setName("B");
        tb.start();
    }
}

class Thread8A extends Thread{
    private MyThread8 m8;

    public Thread8A(MyThread8 m8) {
        this.m8 = m8;
    }

    @Override
    public void run() {
        m8.a();
    }
}

class Thread8B extends Thread{
    private MyThread8 m8;

    public Thread8B(MyThread8 m8) {
        this.m8 = m8;
    }

    @Override
    public void run() {
        m8.b();
    }
}
